﻿using DevExpress.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using 聚欣订单分析.Ling;

namespace 聚欣订单分析.OrderAna
{
    public partial class detailproject : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] == null)
            {
                Response.Redirect("../login.aspx");
            }
            MobileLoad();
            if (ASPxGridView1.IsCallback)
            {
                if (Session["selsql"] != null)
                {
                    this.SqlDataSource1.SelectCommand = Session["selsql"].ToString();
                }
                ASPxGridView1.DataBind();
            }
        }

        private void MobileLoad()
        {
            if (PublicClass.PublicClass.IsMobileLogin())
            {
                ASPxGridView1.Columns["situation"].Visible = false;
                ASPxGridView1.Columns["situationuser"].Visible = false;
                ASPxGridView1.Columns["comment"].Visible = false;
                ASPxGridView1.Columns["commentuser"].Visible = false;
                ASPxGridView1.SettingsPager.PageSize = 10;
                ASPxGridView1.Settings.ShowPreview = true;
            }
        }

        //批注显示
        public string bpz(object pz, object user)
        {
            if (pz.ToString() == "")
            {
                return "";
            }
            return "批注：" + pz.ToString() + "(" + user.ToString() + ")";
        }
        protected void ASPxGridView1_RowValidating(object sender, DevExpress.Web.Data.ASPxDataValidationEventArgs e)
        {
            string strSituation = "";
            if (e.NewValues["situation"] != null)
            {
                strSituation = e.NewValues["situation"].ToString();
            }
            else
            {
                e.RowError = "跟踪情况不能为空！";
                return;
            }
        }

        protected void ASPxGridView1_RowInserted(object sender, DevExpress.Web.Data.ASPxDataInsertedEventArgs e)
        {

        }
        protected void ASPxGridView1_RowUpdated(object sender, DevExpress.Web.Data.ASPxDataUpdatedEventArgs e)
        {
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            //GridViewDataMemoColumn commentnColumn = this.ASPxGridView1.Columns["comment"] as GridViewDataMemoColumn;
            //commentnColumn.EditFormSettings.Visible = DevExpress.Utils.DefaultBoolean.False;
            this.ASPxGridView1.AddNewRow();
        }

        protected void btnDel_Click(object sender, EventArgs e)
        {
            List<object> keyValues = this.ASPxGridView1.GetSelectedFieldValues("ID");
            if (keyValues.Count == 0)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "提示", "<script>alert('请选择删除的数据!')</script>");
                return;
            }
            using (up_GeneralDataContext up_General = new up_GeneralDataContext())
            {
                for (int i = 0; i < keyValues.Count; i++)
                {
                    up_General.up_GeneralDeleteProc("detailstable", "ID=" + keyValues[i]);
                }
            }
            ASPxGridView1.Selection.UnselectAll();
            ASPxGridView1.DataBind();
        }

        //批注
        protected void btnPz_Click(object sender, EventArgs e)
        {
            List<object> keyValues = this.ASPxGridView1.GetSelectedFieldValues("ID");
            if (keyValues.Count == 0)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "提示", "<script>alert('请选择要批注的数据!')</script>");
                return;
            }
            using (up_GeneralDataContext up_General = new up_GeneralDataContext())
            {
                for (int i = 0; i < keyValues.Count; i++)
                {
                    up_General.up_GeneralUpdateProc("detailstable", "comment='" + txtPzs.Value.Trim() + "',commenttime=getdate(),commentuser='" + Session["UserName"].ToString() + "'", "ID=" + keyValues[i]);
                }
            }
            ASPxGridView1.Selection.UnselectAll();
            ASPxGridView1.DataBind();
        }

        //搜索
        protected void btnSel_Click(object sender, EventArgs e)
        {
            string strContent = txtPzs.Value.Trim();

            if (strContent != "")
            {
                Session["selsql"] = "SELECT * FROM [detailstable] WHERE situation like '%" + strContent + "%' or comment like '%" + strContent + "%' and ([projectid] = @projectid) ORDER BY [situationtime] DESC";
            }
            else
            {
                Session["selsql"] = "SELECT * FROM [detailstable] WHERE ([projectid] = @projectid) ORDER BY [situationtime] DESC";
            }
            this.SqlDataSource1.SelectCommand = Session["selsql"].ToString();
            ASPxGridView1.DataBind();
        }
    }
}